import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.future.ReadFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.DatagramSessionConfig;
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
//import org.apache.mina.example.udp.client.MemMonClient;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;

/**
 *
 * @author Aizaz
 */
public class UDPConnector extends IoHandlerAdapter{

    private IoSession session;
    private IoConnector connector;
    private ConnectFuture connFuture;

    public UDPConnector() throws InterruptedException{
        connector = new NioDatagramConnector();
        connector.setHandler(this);
        connFuture = connector.connect(new InetSocketAddress("192.168.3.22",6502));


        connFuture.addListener(new IoFutureListener<ConnectFuture>() {
            public void operationComplete(ConnectFuture future) {
                if (future.isConnected()) {                    
                    session = future.getSession();
                    try {
                        try {
                            sendData();
                          //  connFuture.await();

                        } catch (CharacterCodingException ex) {
                           // java.util.logging.Logger.getLogger(MemMonClient.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } 
            }
        });

    }
    private void sendData() throws InterruptedException, CharacterCodingException {


            IoBuffer buffer = IoBuffer.allocate(8);
            buffer.setAutoExpand(true);
            buffer.putString("any", Charset.forName("UTF-8").newEncoder());
            buffer.flip();
            session.write(buffer);
    }

     @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        cause.printStackTrace();
    }
    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        connFuture.getSession().getConfig().setUseReadOperation(true);

        ReadFuture r = connFuture.getSession().read();
        connFuture.await();
        connFuture.getSession().getConfig().setUseReadOperation(true);


        Object obj = r.getMessage();
        System.out.println("r.getMessage(); "+obj);
        IoBuffer buffer = IoBuffer.allocate(2048);
                        buffer.setAutoExpand(true);                     
                        Object objReceived = buffer.getObject();
                        System.out.println(objReceived.toString());

        System.out.println("reveived Session recv...");
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        System.out.println("Message sent...");
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("Session closed...");
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        System.out.println("Session created...");
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status)
            throws Exception {
        System.out.println("Session idle...");
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("Session opened...");
    }
    public static void main (String are[]){
        try{
        new UDPConnector();
        }catch(Exception ex){ex.printStackTrace();}
    }
}